﻿using System;
using System.Collections;
using System.Collections.Generic;
using UIFrameWork;
using UnityEngine;
/// <summary>
/// 1000 对应 1s
/// </summary>
public class TimeManager : Singleton<TimeManager> {
    public enum enTimeType
    {
        Normal,
        FrameSync,
    }

    private List<Timer>[] m_timers;
    private int m_timerSquence;
    
    public override void Init()
    {
        this.m_timers = new List<Timer>[Enum.GetValues(typeof(enTimeType)).Length];
        for (int i = 0; i < this.m_timers.Length; i++)
        {
            this.m_timers[i] = new List<Timer>();
        }
        this.m_timerSquence = 0;
    }
    
    public override void UnInit()
    {
        
    }

    public int AddTimer(int time, int loop, Timer.OnTimeUpHandler onTimeUpHandler ,bool useFrameSync)
    {
        this.m_timerSquence++;
        this.m_timers[useFrameSync?1:0].Add(new Timer(time,loop,onTimeUpHandler,this.m_timerSquence));
        return this.m_timerSquence;
    }

    public int AddTimer(int time, int loop, Timer.OnTimeUpHandler onTimeUpHandler)
    {
        return AddTimer(time, loop, onTimeUpHandler, false);
    }

    public int AddTimer(int time, int loop, Timer.OnTimeUpWithParamsHandler onTimeUpWithParamsHandler,
        bool useFrameSync, stTimerParams stTimerParams)
    {
        this.m_timerSquence++;
        this.m_timers[useFrameSync?0:1].Add(new Timer(time,loop,onTimeUpWithParamsHandler,this.m_timerSquence,stTimerParams));
        return this.m_timerSquence;
    }

    public int AddTimer(int time, int loop, Timer.OnTimeUpWithParamsHandler onTimeUpWithParamsHandler,
        stTimerParams stTimerParams)
    {
        return AddTimer(time, loop, onTimeUpWithParamsHandler, false, stTimerParams);
    }

    public void RemoveTimer(int squence)
    {
        for (int i = 0; i < m_timers.Length; i++)
        {
            List<Timer> list = m_timers[i];
            for (int j = 0; j < list.Count; j++)
            {
                if (list[j].IsSquenceMatched(squence))
                {
                    list[j].Finish();
                    return;
                }
            }
        }
    }

    public void RemoveTimerSafely(ref int squence)
    {
        if (squence != 0)
        {
            RemoveTimer(squence);
            squence = 0;
        }
    }

    public Timer GetTimer(int sqeunece)
    {
        for (int i = 0; i < m_timers.Length; i++)
        {
            List<Timer> list = m_timers[i];
            for (int j = 0; j < list.Count; j++)
            {
                if (list[j].IsSquenceMatched(sqeunece))
                {
                    return list[j];
                }
            }
        }
        return null;
    }

    public void PauseTimer(int squence)
    {
        Timer timer = GetTimer(squence);
        if (timer != null)
        {
            timer.Pause();
        }
    }

    public void ResumeTimer(int squence)
    {
        Timer timer = GetTimer(squence);
        if (timer != null)
        {
            timer.Resume();
        }
    }

    public void ResetTimer(int squence)
    {
        Timer timer = GetTimer(squence);
        if (timer != null)
        {
            timer.Reset();
        }
    }

    public void ResetTimerTotaltime(int squence, int totaltime)
    {
        Timer timer = GetTimer(squence);
        if (timer != null)
        {
            timer.ResetTotalTime(totaltime);
        }     
    }

    public int GetTimerCurrent(int squence)
    {
        Timer timer = GetTimer(squence);
        if (timer != null)
        {
            return  timer.CurrentTime;
        }
        return -1;
    }

    public int GetLeftTime(int squence)
    {
        Timer timer = GetTimer(squence);
        if (timer != null)
        {
            return timer.GetLeftTime();
        }
        return -1;
    }

    public void RemoveTimer(Timer.OnTimeUpHandler onTimeUpHandler,bool useFrameSync)
    {
        List<Timer> list = m_timers[useFrameSync ? 0 : 1];
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].IsDelegateMatched(onTimeUpHandler))
            {
                list[i].Finish();
            }
        }
    }

    public void RemoveTimer(Timer.OnTimeUpHandler onTimeUpHandler)
    {
        RemoveTimer(onTimeUpHandler,false);
    }

    public void RemoveTimer(Timer.OnTimeUpWithParamsHandler onTimeUpWithParamsHandler, bool useFraesync)
    {
        List<Timer> list = m_timers[useFraesync ? 0 : 1];
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].IsDelegateParamsMatched(onTimeUpWithParamsHandler))
            {
                list[i].Finish();
            }
        }
    }

    public void RemoveTimer(Timer.OnTimeUpWithParamsHandler onTimeUpWithParamsHandler)
    {
        RemoveTimer(onTimeUpWithParamsHandler,false);
    }

    public void RemoveAllTimer(bool useFrameSync)
    {
        List<Timer> list = m_timers[useFrameSync ? 0 : 1];
        list.Clear();
    }

    public void RemoveAllTimer()
    {
        for (int i = 0; i < m_timers.Length; i++)
        {
            m_timers[i].Clear();
        }
    }

    public void UpdateLogic(int delta)
    {
        UpdateTimer(delta,enTimeType.FrameSync);
    }

    public void UpdateTimer(int delta,enTimeType enTimeType)
    {
        List<Timer> list = this.m_timers[(int) enTimeType];
        int i = 0;
        while (i<list.Count)
        {
            if (list[i].IsFinished())
            {
                list.RemoveAt(i);
            }
            else
            {
                list[i].Update(delta);
                i++;
            }
        }
    }

    public void Update()
    {
        this.UpdateTimer((int) (Time.deltaTime*1000),enTimeType.Normal);
    }
    
}







































